<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class WechatController extends Controller
{
    //
    public function serve()
    {
        Log::info('request arrived.');

        $wechat = app('wechat');
        $userApi = $wechat->user;
        $wechat->server->setMessageHandler(function($message)use($userApi,$wechat){
          switch ($message->MsgType) {
              case 'event':
                  // 事件消息...
                  if($message->Event == 'caseok'){
                    return 'okok';
                  }
                  if($message-Event == 'CLICK'){
                    if($message->Eventkey == 'CLICK_ME'){
                      return 'CLICK_ME !!!';
                    }
                    if($message->Eventkey == 'ON_LIKE'){
                      return 'ON_LIKE !!!';
                    }
                  }
                  break;
              case 'text':
                  // 文字消息...
                  return 'Hello!'.$userApi->get($messages->FromUserName)->nikename;
                  break;
              case 'image':
                  // 图片消息...
                  $image = new Image(['media_id'=>'']);
                  $wechat->staff->message($image)->to($message->FromUserName)->send();
                  break;
              case 'voice':
                  // 语音消息...
                  $voice = new voice(['media_id'=>'']);
                  $wechat->staff->message($voice)->to($message->FromUserName)->send();
                  break;
              case 'video':
                  // 视频消息...
                  $video = new Video(['media_id'=>'']);
                  $wechat->staff->message($video)->to($message->FromUserName)->send();
                  break;
              case 'location':
                  // 坐标消息...
                  break;
              case 'link':
                  // 链接消息...
                  break;
              // ... 其它消息
              default:
                  // code...
                  break;
          }
        });

        Log::info('return response.');

        return $wechat->server->serve();
    }
}
